﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using System.Globalization;
using NLBS.Entity;

namespace NLBS.Data.Business
{
   public class CategoryData:CommonBase
    {
        private static readonly string CategoryKey = "CategoryKey";

        public static List<Category> GetCategory()
        {
            string key = string.Format(CultureInfo.InvariantCulture, CategoryKey);
            ContentCache cache = ContentCache.Instantiate();
            List<Category> lists=(List<Category>)cache[key];
            if (lists == null)
            {
                lists =new List<Category>(DbProvider.Instance().GetCategory());
                if (lists != null)
                {
                    cache.Insert(key, lists, CacheDuration.Long);
                }
            }
            return lists;
        }

        public static List<Category> CategoryList
        {
            get
            {
                return GetCategory();
            }
        }
        public static List<Category> GetCategoryList()
        {
            CategoryList.Sort(Compare);
            return CategoryList;
        }

        public static Category GetCategoryByID(int id)
        {
            return CategoryList.Find(delegate(Category c)
            {
                return c.CategoryID == id;
            });

        }

        public static void InsertCategory(Category cat)
        {
            int result = DbProvider.Instance().InsertCategory(cat);
            cat.CategoryID = result;
            CategoryList.Add(cat);
        }

       public static int Compare(Category x, Category y)
       {
           if (x == null)
           {
               if (y == null)
               {

                   return 0;
               }
               else
               {

                   return -1;
               }
           }
           else
           {

               if (y == null)
               {
                   return 1;
               }
               else
               {

                   int retval = x.Order.CompareTo(y.Order);

                   if (retval != 0)
                   {
                       return retval;
                   }
                   else
                   {
                       return x.Order.CompareTo(y.Order);
                   }
               }
           }
       }

       public static void UpdateCategory(Category cat)
       {
           DbProvider.Instance().UpdateCategory(cat);
           //Category oldCat = GetCategoryByID(cat.CategoryID);
          // IList<Category> list = GetCategory();
          // list[list.IndexOf(oldCat)] = cat;
       }

       public static void DeleteCategory(int catID)
       {
           DbProvider.Instance().DeleteCategory(catID);
           
           CategoryList.Remove(GetCategoryByID(catID));
           
       }
    }
}
